<?php

namespace app\admin\model\workteam;

use think\Model;
use traits\model\SoftDelete;

class Statement extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'workteam_statement';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    // 追加属性
    protected $append = [

    ];


    public function getStatusList()
    {
        return ['1' => __('正常'), '0' => __('生成中'), '2' => __('待确认')];
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : ($value ? $value : null);
    }


    /**
     * 归属者
     */
    public function admin()
    {
        return $this->belongsTo("\\app\\admin\\model\\Admin", 'admin_id');
    }

    /**
     * 获取指定年月的开始和结束时间戳
     * @param int $y    年份
     * @param int $m    月份
     * @return array(开始时间,结束时间)
     */
    public static function  mFristAndLast($y=0,$m=0){
        $y = $y ? $y : date('Y');
        $m = $m ? $m : date('m');
        $d = date('t', strtotime($y.'-'.$m));
        return array("start_time"=>strtotime($y.'-'.$m),"end_time"=>mktime(23,59,59,$m,$d,$y));
    }

    /**
     * 统计并生成报表数据
     */
    public  static function  statistics($year=0,$month=0,$admin_id){

        $tarr=self::mFristAndLast($year,$month);
        $taskModel=new Task();
        $data=array();
        //获取月要开始任务数量和可得工时 [只算任务计划开始时间]

        $plan_task_info = $taskModel->where('start_time', 'between time', [$tarr['start_time'], $tarr['end_time']])
            ->field('COUNT(*) AS plan_task, sum(get_hours) as plan_hours')
            ->where('status','<>','-1')
            ->where('admin_id',$admin_id)
            ->find();
        $plan_task_info=($plan_task_info->toArray());
        $data=array_merge($data,$plan_task_info);

        //获取月完成任务数量和可得工时 [只算任务计划开始时间]
        $complete_task_info=$taskModel->where('complete_time', 'between time', [$tarr['start_time'], $tarr['end_time']])
            ->field('COUNT(*) AS complete_task, sum(get_hours) as get_hours')
            ->where('admin_id',$admin_id)
            ->where('status','2')->find();
        $complete_task_info=$complete_task_info->toArray();
        $data=array_merge($data,$complete_task_info);

        //任务逾期数量
        $data['overdue']=$taskModel->where('end_time', 'between time', [$tarr['start_time'], $tarr['end_time']])
            ->where('admin_id',$admin_id)
            ->where('status','in','0,1')->count();
        //TODO 业绩暂时不算，需要算出所有项目的贡献率，然后把项目业绩算出来

        $data['status']=2;//标为确认状态
        $data['year']=$year;
        $data['month']=$month;
        $data['admin_id']=$admin_id;
        //判断是否生成过
        $selfModel=new self();
        $row=$selfModel->where('month',$month)->where('year',$year)->where('admin_id',$admin_id)->find();
        if ($row){
            if ($row['status']==1) return  false;//已经确定的就不能再
            return $row->allowField(true)->save($data);
        }

        return $selfModel->isUpdate(false)->allowField(true)->save($data);

    }

}
